<template>
  <div>
    <input placeholder="输入关键字搜索" v-model="searchQuery">
    <MyTable :records="filteredRecords">
      <template #head>
        <th>就诊日期</th>
        <th>医生姓名</th>
        <th>诊断结果</th>
        <th>处方信息</th>
        <th>操作</th>
      </template>
      <template #body="{ item, index }">
        <td>{{ index + 1 }}</td>
        <td>{{ item.doctor }}</td>
        <td>{{ item.diagnosis }}</td>
        <td>{{ item.prescription }}</td>
        <td @click="handleDetail(item)">详情</td>
      </template>
    </MyTable>
    <MyDetail v-show="isShow" :item="item" @close="isShow = false" :isShow="isShow" v-if="item">
    </MyDetail>
  </div>
</template>
<script>
import MyDetail from './components/MyDetail.vue';
import MyTable from './components/MyTable.vue';
export default {
  components: {
    MyTable,
    MyDetail
  },
  data() {
    return {
      isShow: false,
      searchQuery: '',
      item: null,
      records: [
        {
          date: '2022-01-01',
          doctor: '张三',
          diagnosis: '感冒',
          prescription: '感冒药',
        },
        {
          date: '2022-02-01',
          doctor: '李四',
          diagnosis: '头疼',
          prescription: '止疼药',
        },
        {
          date: '2022-03-01',
          doctor: '王五',
          diagnosis: '腰痛',
          prescription: '止痛贴',
        }
      ]
    }
  },
  methods: {
    onSearchChange() {
      console.log("Search Query:", this.searchQuery);
    },
    handleDetail(item) {
      if (item) {
        this.item = item;
        this.isShow = true;
        // console.log(item);
      }
    }
  },
  computed: {
    // 使用计算属性来筛选记录
    filteredRecords() {
      const searchTerm = this.searchQuery.toLowerCase();
      return this.records.filter(record => {
        return (
          record.doctor.toLowerCase().includes(searchTerm) ||
          record.diagnosis.toLowerCase().includes(searchTerm) ||
          record.prescription.toLowerCase().includes(searchTerm)
        );
      });
    },
  },
}
</script>